package com.leyou.trade.web;

import com.leyou.trade.dto.OrderDTO;
import com.leyou.trade.dto.OrderFormDTO;
import com.leyou.trade.entity.Order;
import com.leyou.trade.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    /**
     * 创建订单
     * @param orderFormDTO  订单数据
     * @return  订单id
     * @Valid 服务端进行数据校验基于Hibernate Validator经常用来验证bean的字段
     */
    @PostMapping
    public ResponseEntity<Long>createOrder(@RequestBody @Valid OrderFormDTO orderFormDTO){

        Long orderId = orderService.createOrder(orderFormDTO);
        return ResponseEntity.status(HttpStatus.CREATED).body(orderId);
    }

    /**
     * 根据id查询订单
     * @param orderId 订单id
     * @return 订单对象
     */
    @GetMapping("/{id}")
    public ResponseEntity<OrderDTO> queryOrderById(@PathVariable("id") Long orderId){
        OrderDTO orderDTO = orderService.getByOrderId(orderId);
        return ResponseEntity.ok(orderDTO);
    }

    @GetMapping("/status/{id}")
    public ResponseEntity<Integer> queryOrderStatusById(@PathVariable("id") Long orderId){
        OrderDTO orderDTO = orderService.getByOrderId(orderId);
        return ResponseEntity.ok(orderDTO.getStatus());
    }
}
